package com.itlzh.mall.user.serviceTest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 用户代理类
 */
public class UserServiceProxy {
    Object target;

    public UserServiceProxy(Object target) {
        this.target = target;
    }

    /**
     * 获取代理类
     * @return
     */
    public Object getProxy(){
        ClassLoader classLoader=target.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //身份检查
                Assistant.authentication();
                //执行业务方法
                Object o = method.invoke(target, args);
//                Assistant.doLog(method.getName());
                return o;
            }
        };
        return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
    }
}
